<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Content_testimoni extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_content_testimoni');

		//library breadcrum/untuk navigasi
		$this->load->library('breadcrumb');
		$this->path = './foto_testimoni/';
	}

	function index() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Data Testimoni');

		//meta description jika perlu
		$this->layout->set_desc('Data Testimoni');

		//css tambahan bila perlu
		$this->layout->set_include('css', 'themes/back/css/datatables/dataTables.bootstrap.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Testimoni', site_url('content_testimoni'));
		

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load library "paging"
		$config['sql']      				= $this->m_content_testimoni->get();//Query SQL - bukan array lho ya...
		$config['per_page'] 				= 15;                            //jumlah item tiap halaman
		$data = $this->paging->create_pagination($config);   				//Load fungsi di library "paging" untuk membuat pagination. 

		//judul besar
		$data['primary_title'] 			= 'Content';
		$data['sub_primary_title'] 	= '';

		//menghilangka session jika ada
		$this->session->unset_userdata('keyword');

		//menggunakan theme back
		$this->layout->theme('back','content_testimoni/index', $data);
	}

	function cari() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Cari Data testimoni');

		//meta description jika perlu
		$this->layout->set_desc('Data testimoni');

		//css tambahan bila perlu
		$this->layout->set_include('css', 'themes/back/css/datatables/dataTables.bootstrap.css');

		//js tambahan jika perlu
		$this->layout->set_include('js', 'themes/back/js/plugins/datatables/jquery.dataTables.js');
		$this->layout->set_include('js', 'themes/back/js/plugins/datatables/dataTables.bootstrap.js');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Testimoni', site_url('content_testimoni'));
		//$this->breadcrumb->add_crumb('Barang');	

		//buat cari
		if(isset($_POST['keyword'])){
			$data['keyword'] = $this->input->post('keyword');
			$this->session->set_userdata('keyword', $data['keyword']);
		} else {
			$data['keyword'] = $this->session->userdata('keyword');
		}	

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load the "paging" library
		$config['sql']      				= $this->m_content_testimoni->get_cari($data['keyword']);//your SQL - bukan array lho ya...
		$config['per_page'] 				= 15;                            //Display items per page
		$data = $this->paging->create_pagination($config);   				//Load function in "paging" libraryfor create pagination. 

		//judul besar
		$data['primary_title'] 			= 'Content';
		$data['sub_primary_title'] 	= '';

		//menggunakan theme back
		$this->layout->theme('back','content_testimoni/index', $data);
	}

	function detail($id_testimoni = '') {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//parameter yang dipelukan query
		$params = array($id_testimoni);

		//Layout
		//title
		$this->layout->set_title('Detail Testimoni');

		//meta description jika perlu
		//$this->layout->set_desc($this->m_master_barang->get_ket($params));

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Testimoni', site_url('content_testimoni'));
		$this->breadcrumb->add_crumb('Detail Testimoni');

		//judul besar
		$data['primary_title'] 			= 'Content';
		$data['sub_primary_title'] 	= '';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] = $this->m_content_testimoni->get_by($params);

		//menggunakan theme back
		$this->layout->theme('back','content_testimoni/detail', $data);
	}

	function add() {

		//rule type pada function ini: Create
		$this->rule->type('C');

		//Layout
		//title
		$this->layout->set_title('Tambah Data Testimoni');

		//js tambahan
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Testimoni', site_url('content_testimoni'));
		$this->breadcrumb->add_crumb('Tambah Testimoni');

		//judul besar
		$data['primary_title'] = 'Content';
		$data['sub_primary_title'] = '';

		//menggunakan theme back
		$this->layout->theme('back','content_testimoni/add', $data);
	}

	function save() {

		//rule type pada function ini: Create
		$this->rule->type('C');
		
		//upload gambar
		if(!empty($_FILES['gambar']['name'])){
			//Konfigurasi upload gambar
			$config = array(
			'allowed_types'=>'jpg|jpeg|gif|png',
			'upload_path' => $this->path,
			'max_size' => 2000);

			$this->load->library('upload',$config);
			$status_upload=$this->upload->do_upload('gambar');
			$image_data = $this->upload->data();

			//Resize gambar
			$config = array(
				'source_image'=> $image_data['full_path'],
				'new_image'=>$this->path . '/thumbs',
				'maintain_ration'=>true,
				'width'=>120,
				'height'=>120
			);

			$this->load->library('image_lib', $config);
			$this->image_lib->resize();

			$nama_gambar=$image_data['file_name'];
		}else{
			$nama_gambar="default.jpg";
			
		}
		$dataInsert = array(
			'testimoni' => $this->input->post('testimoni'),
			'nama' 			=> $this->input->post('nama'), 
			'pekerjaan' => $this->input->post('pekerjaan'), 
			'foto' 		=> $nama_gambar
		);

		if(!$this->m_content_testimoni->save($dataInsert)){
			$this->session->set_flashdata('gagal', 'Testimoni <b>'.$this->input->post('nama').'</b> gagal ditambahkan.');
			redirect('content_testimoni','refresh');
		}else{
			$this->session->set_flashdata('sukses', 'Testimoni <b>'.$this->input->post('nama').'</b> berhasil ditambahkan.');
			redirect('content_testimoni','refresh');
		}

		
	}

	function edit($id_testimoni = '') {

		//rule type pada function ini: Update
		$this->rule->type('U');

		//parameter yang dipelukan query
		$params = array($id_testimoni);

		//layout
		//title
		$this->layout->set_title('Edit Data Testimoni');

		//js tambahan
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('testimoni', site_url('content_testimoni'));
		$this->breadcrumb->add_crumb('Edit testimoni');

		//judul besar
		$data['primary_title'] 			= 'Services';
		$data['sub_primary_title'] 	= '';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] = $this->m_content_testimoni->get_by($params);

		//menggunakan theme back
		$this->layout->theme('back','content_testimoni/edit', $data);
	}

	function update() {

		//rule type pada function ini: Update
		$this->rule->type('U');
		
		//upload gambar
		if(!empty($_FILES['foto']['name'])){
			$config = array(
				'allowed_types'=>'jpg|jpeg|gif|png',
				'upload_path' => $this->path,
				'max_size' => 2000
			);

			$this->load->library('upload',$config);
			$status_upload=$this->upload->do_upload('foto');
			$image_data = $this->upload->data();
			//resize gambar
			$config = array(
				'source_image'=> $image_data['full_path'],
				'new_image'=>$this->path . '/thumbs',
				'maintain_ration'=>true,
				'width'=>160,
				'height'=>120
			);

			$this->load->library('image_lib', $config);
			$this->image_lib->resize();
			$nama_gambar=$image_data['file_name'];
			$foto = $this->input->post('tmp_foto');
			if($foto != 'default.jpg'){
				unlink($this->path.'/thumbs/'.$foto);
				unlink($this->path.'/'.$foto);
			}
		}else{
			$nama_gambar=$this->input->post('tmp_foto');
		}

		$dataUpdate = array(
			'testimoni' => $this->input->post('testimoni'),
			'nama' 			=> $this->input->post('nama'), 
			'pekerjaan' => $this->input->post('pekerjaan'), 
			'foto' 			=> $nama_gambar
		);

		$id_testimoni = $this->input->post('id_testimoni');
		
		if(!$this->m_content_testimoni->update($id_testimoni, $dataUpdate)){
			$this->session->set_flashdata('gagal', 'Testimoni <b>'.$this->input->post('nama').'</b> gagal diperbaharui.');
			redirect($this->input->post('redirurl'),'refresh');
		}else{
			$this->session->set_flashdata('sukses', 'Testimoni <b>'.$this->input->post('nama').'</b> berhasil diperbaharui.');
			redirect($this->input->post('redirurl'),'refresh');
		}
	}

	function delete($id_testimoni = '') {

		//rule type pada function ini: Delete
		$this->rule->type('D');
		$data = $this->m_content_testimoni->get_by($id_testimoni);
		$foto = $data['foto'];

		if(!$this->m_content_testimoni->delete($id_testimoni)){
			$this->session->set_flashdata('gagal', 'Data gagal dihapus.');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}else{
			if($foto != 'default.jpg'){
				unlink($this->path.'/thumbs/'.$foto);
				unlink($this->path.'/'.$foto);
			}
			$this->session->set_flashdata('sukses', 'Data berhasil dihapus.');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}
	}

}
